package com.wl.spring.mvc.web;

import com.wl.spring.mvc.entity.Result;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.multipart.MaxUploadSizeExceededException;

//主键注解
@Component
//控制器增强注解
//RestController 前缀表示该类所有的方法返回 json 数据
@RestControllerAdvice
public class ExAdvice {
    @ExceptionHandler(MaxUploadSizeExceededException.class)
    public Result MaxUploadSizeExceededException(MaxUploadSizeExceededException ex) {
        String msg = "上传文件大小不能超过 %s";
        msg = String.format(msg, "1MB");  //自己定义的上传文件大小不能超过一兆
        return new Result(0, msg, null);
    }

    @ExceptionHandler(Exception.class)
    public Result exception(Exception e) {
        e.printStackTrace();
        return new Result(-1, "系统繁忙，请稍后再试", null);
    }
}
